package com.cwj.mansysback.service.impl;

import com.cwj.mansysback.dao.api.CourseDao;
import com.cwj.mansysback.dao.api.ElectiveDao;
import com.cwj.mansysback.dao.api.StudentDao;
import com.cwj.mansysback.dao.api.TeacherDao;
import com.cwj.mansysback.dao.impl.CourseDaoImpl;
import com.cwj.mansysback.dao.impl.ElectiveDaoImpl;
import com.cwj.mansysback.dao.impl.StudentDaoImpl;
import com.cwj.mansysback.dao.impl.TeacherDaoImpl;
import com.cwj.mansysback.entity.*;
import com.cwj.mansysback.service.api.ElectiveService;

import java.util.Arrays;
import java.util.List;

public class ElectiveServiceImpl implements ElectiveService {

    private static final ElectiveDao electiveDao = new ElectiveDaoImpl();

    private static final CourseDao courseDao = new CourseDaoImpl();

    private static final StudentDao studDao = new StudentDaoImpl();

    private static final TeacherDao teaDao = new TeacherDaoImpl();

    @Override
    public boolean insertElective(String cid, String sid, double cscore) {
        return electiveDao.insertElective(cid, sid, cscore);
    }

    @Override
    public boolean updateEletive(String cid, String sid, double cscore) {
        return electiveDao.updateEletive(cid,sid,cscore);
    }

    @Override
    public boolean deleteElective(String cid, String sid) {
        return electiveDao.deleteElective(cid, sid);
    }

    @Override
    public Elective selectElective(String cid, String sid) {

        return electiveDao.selectElective(cid, sid);
    }

    @Override
    public List<Elective> selectCidElective(String cid, int start, int len) {

//        System.out.println("getCourse");
        List<Elective> list = electiveDao.selectCidElective(cid, start, len);

//        System.out.println(Arrays.toString(list.toArray()));

        return this.change(list, true, false);
    }

    @Override
    public List<Elective> selectSidElective(String sid, int start, int len) {
        List<Elective> list = electiveDao.selectSidElective(sid, start, len);

        return this.change(list, false, true);
    }

    @Override
    public List<Elective> selectElective(int start, int len) {
        List<Elective> list = electiveDao.selectElective(start, len);

        return this.change(list, false, false);
    }

    private List<Elective> change(List<Elective> list, boolean isCid, boolean isSid) {
        Elective e = null;

        for (Elective elective : list) {

            e = elective;

            if (!isSid) {
                Student st = null;
                e.setStud(st = studDao.selectStudent(e.getSid()));
                st.lastChange();
            }

            if (!isCid) {
                Teacher t = null;
                e.setCourse(courseDao.selectCourse(e.getCid()));
                e.getCourse().setTeacher(t = teaDao.selectTeacher(e.getCourse().getTid()));
                t.lastChange();
            }


            System.out.println(e);
        }


        return list;
    }

    @Override
    public Pack selectPackBySid(String sid) {
        return electiveDao.selectPackBySid(sid);
    }

    @Override
    public Pack selectPackByCid(String cid) {
        return electiveDao.selectPackByCid(cid);
    }

    @Override
    public Pack selectPack() {
        return electiveDao.selectPack();
    }


}
